package m202405.d13;

import java.util.ArrayList;
import java.util.List;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/13 15:56
 */
public class Q114 {
    public void flatten(TreeNode root) {
        if (root == null) {
            return;
        }
        List<TreeNode> list = new ArrayList<>();
        travel(root, list);
        root.left = null;
        for (int i = 1; i < list.size(); i++) {
            TreeNode c = list.get(i);
            c.left = null;
            root.right = c;
            root = root.right;
        }
    }

    private void travel(TreeNode node, List<TreeNode> list) {
        if (node == null) {
            return;
        }
        list.add(node);
        travel(node.left, list);
        travel(node.right, list);
    }
}
